package com.fly.tools;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.fly.entity.po.Course;
import com.fly.entity.po.CourseReservation;
import com.fly.entity.po.School;
import com.fly.entity.query.CourseQuery;
import com.fly.service.ICourseReservationService;
import com.fly.service.ICourseService;
import com.fly.service.ISchoolService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.List;


@RequiredArgsConstructor
@Component
public class CourseTools {

    private final ICourseService courseService;
    private final ICourseReservationService courseReservationService;
    private final ISchoolService schoolService;

    @Tool(description = "根据条件查询课程")
    public List<Course> queryCourse(@ToolParam CourseQuery courseQuery) {
        // 判断查询对象是否为空
        if (courseQuery == null) {
            return courseService.list();
        }
        // 构建查询条件
        QueryChainWrapper<Course> le = courseService.query()
                .eq(courseQuery.getType() != null, "type", courseQuery.getType())
                .le(courseQuery.getEdu() != null, "edu", courseQuery.getEdu());
        if (courseQuery.getSorts() != null && !courseQuery.getSorts().isEmpty()) {
            for (CourseQuery.Sort sort : courseQuery.getSorts()) {
                le.orderBy(true, sort.getAsc(), sort.getField());
            }
        }
        // 返回查询结果
        return le.list();
    }

    @Tool(description = "查询全部校区")
    public List<School> querySchool() {
        return schoolService.list();
    }

    @Tool(description = "生成预约单，返回预约单号")
    public Integer createCourseReservation(
            @ToolParam(description = "预约课程") String course,
            @ToolParam(description = "预约小区") String school,
            @ToolParam(description = "学生姓名") String studentName,
            @ToolParam(description = "联系电话") String contactInfo,
            @ToolParam(description = "备注", required = false) String remark
    ) {
        CourseReservation courseReservation = new CourseReservation();
        courseReservation.setCourse(course);
        courseReservation.setSchool(school);
        courseReservation.setStudentName(studentName);
        courseReservation.setContactInfo(contactInfo);
        if (remark != null) {
            courseReservation.setRemark(remark);
        }
        return courseReservationService.save(courseReservation) ? courseReservation.getId() : null;
    }
}
